package com.iceclean.daoImpl;

import com.iceclean.dao.MessageDao;
import com.iceclean.po.Message;
import com.iceclean.util.JdbcUtil;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-09
 *
 * 消息表操作实现类
 */
public class MessageDaoImpl implements MessageDao {

    @Override
    public Message findMessageById(int messageId) {
        List<Message> messageList = JdbcUtil.query("select * from t_message where message_id = ?", Message.class, messageId);
        return messageList.size() == 0 ? null : messageList.get(0);
    }

    @Override
    public List<Message> findMessageByPage(int userId, int messageClass, int page, int num) {
        List<Message> messageList = JdbcUtil.query("select * from t_message where receiver_id = ? and message_class = ? order by create_date desc limit " + (page-1)*num + "," + num, Message.class, userId, messageClass);
        return messageList.size() == 0 ? null : messageList;
    }

    @Override
    public int getAllMessageForNumber(int userId, int messageClass) {
        return JdbcUtil.query("select message_id from t_message where receiver_id = ? and message_class = ?", Message.class, userId, messageClass).size();
    }

    @Override
    public int createMessage(Message message) {
        return JdbcUtil.insert("t_message", message);
    }
}
